package com.springboot.demo.queue;

/**
 * 同步队列
 *
 * @author supu
 * @date 2019-04-10 16:19
 **/
public class SynchronizedQueue<T> {
    private static final int DEFAULT_SIZE = 128;

    private int size;
    private int insert = 0;
    private int remove = 0;

    private Object[] queue;

    public SynchronizedQueue() {
        this(DEFAULT_SIZE);
    }

    public SynchronizedQueue(int size) {
        this.size = size;
        queue = new Object[size];
    }

    public synchronized boolean offer(T object) {
        queue[insert++] = object;

        // 插满数据后，重置指针到初始值继续往队列中添加数据
        if (insert == size) {
            insert = 0;
        }

        if (insert == remove) {
            // 当insert指针和remove指针到达同一个地方时，说明队列满了，此时需要拓展队列内存
            expand();
        }

        return true;
    }

    public synchronized T poll() {
        if (insert == remove) {
            return null;
        }

        T result = (T) queue[remove];
        queue[remove] = null;
        remove++;

        // 当remove指针到达size时，需要重置到初始值0，以便重新移除queue对象
        if (remove == size) {
            remove = 0;
        }

        return result;
    }

    public synchronized int size() {
        int result = insert - remove;
        if (result < 0) {
            result += size;
        }
        return result;
    }

    public synchronized void clear() {
        // 这种方式是重新定义一个新数组
//        queue = new Object[size];
        //还有一种方式是，清空queue中对象
        for (int i = 0; i < size(); i++) {
            queue[i] = null;
        }
        insert = 0;
        remove = 0;
    }


    private void expand() {
        System.out.println("expand is run ,size:" + size);
        int newSize = size * 2;
        Object[] newQueue = new Object[newSize];

        System.arraycopy(queue, insert, newQueue, 0, size - insert);
        System.arraycopy(queue, 0, newQueue, size - insert, insert);

        insert = size;
        remove = 0;
        queue = newQueue;
        size = newSize;
    }

}
